'''
电流环 大屏展示 数据查询统计服务
'''
from pymongo import MongoClient
from pao import log
from gov.data_process.dlh_screen_display.stat_data import DataProcessStat
from gov.data_process.dlh_screen_display.sim_data_every2min_equip import DataProcessEvery2mins
from flask import session


def register(jsonrpc, db_addr, db_port):
    data_every2min = DataProcessEvery2mins(db_addr, db_port)
    data_stat = DataProcessStat(db_addr, db_port)

    @jsonrpc.method('getdata.zgl')
    def zgl_service(date):
        nonlocal data_stat
        res = data_stat.cal_zgl(date)
        return res

    @jsonrpc.method('getdata.dlz')
    def dlz_service(date1, date2, dlh_num):
        nonlocal data_every2min
        # 计算当前单一电流环的实时电流值；输入参数为开始时间，结束时间和电流环编号，其中日期为字符型，电流环编号为int
        res = data_every2min.get_dlz(date1, date2, dlh_num)
        return res

    @jsonrpc.method('getdata.equipdata')
    def equip_dlhdata_service(date1, date2, dlh_num):
        nonlocal data_every2min
        # 返回当前单一电流环的实测电流波，并拆分为多个电器的电流波；输入参数为开始时间，结束时间和电流环编号，其中日期为字符型，电流环编号为int
        res = data_every2min.get_equip_dlh_list(date1, date2, dlh_num)
        return res

    @jsonrpc.method('getdata.dwsx_situation')
    def dwsx_situation_service():
        nonlocal data_stat
        # 计算situation中的单位属性图数据，当前时间前24小时不同类型单位属性的用电量
        res = data_stat.cal_dwsx_situation()
        return res

    @jsonrpc.method('getdata.dqsx_situation')
    def dqsx_situation_service():
        nonlocal data_stat
        # 计算situation中的电器类型图数据，当前时间前24小时不同类型单位属性的用电量
        res = data_stat.cal_dqsx_situation()
        return res

    @jsonrpc.method('getdata.ydl24_situation')
    def ydl24_situation_service():
        nonlocal data_stat
        # 计算situation中的电器类型图数据，当前时间前24小时不同类型单位属性的用电量
        res = data_stat.cal_ydl24_situation()
        return res

    @jsonrpc.method('getdata.dwsx_analysis')
    def dwsx_analysis_service():
        nonlocal data_stat
        # 计算analysis中的单位属性的饼图数据，前一天24小时的数据统计
        res = data_stat.cal_dwsx_analysis()
        return res

    @jsonrpc.method('getdata.dqsx_analysis')
    def dqsx_analysis_service():
        nonlocal data_stat
        # 计算analysis中的电器类型的饼图数据，前一天24小时的数据统计
        res = data_stat.cal_dqsx_analysis()
        return res

    @jsonrpc.method('getdata.ydjt_analysis')
    def ydjt_analysis_service():
        nonlocal data_stat
        # 计算analysis中的用电阶梯的柱形图数据，前一天24小时的数据统计
        res = data_stat.cal_ydjt_analysis()
        return res

    @jsonrpc.method('getdata.gfydl_analysis')
    def gfydl_analysis_service():
        nonlocal data_stat
        # 计算analysis中的高峰用电量柱形图数据，前一天24小时的数据统计
        res = data_stat.cal_gfydl_analysis()
        return res

    @jsonrpc.method('getdata.equip_proprietor')
    def equip_proprietor_service(dlh_num):
        nonlocal data_stat
        # 计算proprietor中某个用户的各电器月度用电量统计
        res = data_stat.cal_equip_proprietor(dlh_num)
        return res

    @jsonrpc.method('getdata.loc_ydl_analysis')
    def loc_ydl_analysis_service():
        nonlocal data_stat
        # 计算analysis中地图热力图数据，统计前一天24小时各电流环的用电量
        res = data_stat.cal_loc_ydl_analysis()
        return res

    @jsonrpc.method('getdata.return_all_collection_data')
    def return_all_collection_data_service(collectionname):
        nonlocal netthing_app
        # 返回指定collection的全表内容
        res = netthing_app.return_all_collection_data(collectionname)
        return res

    @jsonrpc.method('getdata.return_netthing_object')
    def return_netthing_object_service(userid):
        nonlocal netthing_app
        # 返回指定userid绑定的所有物联网对象的信息
        res = netthing_app.return_netthing_object(userid)
        return res

    @jsonrpc.method('ILoginService.Login_login')
    def Login_login_service(user_name, password, platform_type, platform_version, ip):
        nonlocal netthing_app
        # 登录确认，返回结果，并修改登录时间，增加登录记录
        app_type = '后台管理web'
        res = netthing_app.login_confirm(
            user_name, password, platform_type, platform_version, ip, app_type)
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_emerg')
    def Data_monitor_stat_emerg_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'EMERGENT_LEVEL': '重大事故'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_processing')
    def Data_monitor_stat_processing_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'STATE': '处理中'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_stat_untreated')
    def Data_monitor_stat_untreated_service():
        nonlocal netthing_app
        collection_name = 'PT_NETTHING_ACCIDENT_MONITOR'
        condition = {'STATE': '未处理'}  # 条件可待确认后修改
        res = netthing_app.stat_event_number(collection_name, condition)
        return res

    @jsonrpc.method('IDataService.Data_monitor_map')
    def Data_monitor_map_service():
        nonlocal netthing_app
        res = netthing_app.query_Data_monitor_map()
        return res

    @jsonrpc.method('IDataService.Data_monitor_list')
    def Data_monitor_list_service():
        nonlocal netthing_app
        res = netthing_app.query_Data_monitor_map()
        return res
